/*     */ package com.zimbra.cs.imap;
/*     */ 
/*     */ import java.text.DateFormat;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ import java.util.TreeSet;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ abstract class ImapSearch
/*     */ {
/*     */   abstract boolean canBeRunLocally();
/*     */   
/*     */   abstract String toZimbraSearch(ImapFolder paramImapFolder)
/*     */     throws ImapParseException;
/*     */   
/*     */   abstract ImapMessage.ImapMessageSet evaluate(ImapFolder paramImapFolder)
/*     */     throws ImapParseException;
/*     */   
/*  34 */   boolean requiresMODSEQ() { return false; }
/*     */   
/*     */   static boolean isAllMessages(ImapFolder i4folder, Set<ImapMessage> i4set) {
/*  37 */     int size = i4set.size() - (i4set.contains(null) ? 1 : 0);
/*  38 */     return size == i4folder.getSize();
/*     */   }
/*     */   
/*     */   static String sequenceAsSearchTerm(ImapFolder i4folder, TreeSet<ImapMessage> i4set, boolean abbreviateAll) {
/*  42 */     i4set.remove(null);
/*  43 */     if (i4set.isEmpty())
/*  44 */       return "item:none";
/*  45 */     if ((abbreviateAll) && (isAllMessages(i4folder, i4set))) {
/*  46 */       return "item:all";
/*     */     }
/*     */     
/*  49 */     StringBuilder sb = new StringBuilder("item:{");
/*  50 */     for (ImapMessage i4msg : i4set) {
/*  51 */       sb.append(sb.length() == 6 ? "" : ",").append(i4msg.msgId);
/*     */     }
/*  53 */     return '}';
/*     */   }
/*     */   
/*     */   static String stringAsSearchTerm(String content) {
/*  57 */     return stringAsSearchTerm(content, true);
/*     */   }
/*     */   
/*     */   static String stringAsSearchTerm(String content, boolean wildcard) {
/*  61 */     String sanitized = content.replace('*', ' ').replace('"', ' ');
/*  62 */     if ((wildcard) && ((sanitized.length() == 0) || (!Character.isWhitespace(sanitized.charAt(sanitized.length() - 1))))) {
/*  63 */       sanitized = sanitized + '*';
/*     */     }
/*  65 */     return '"' + sanitized + '"';
/*     */   }
/*     */   
/*     */   static abstract class LogicalOperation extends ImapSearch
/*     */   {
/*  70 */     List<ImapSearch> mChildren = new ArrayList();
/*     */     
/*     */     LogicalOperation addChild(ImapSearch i4search) {
/*  73 */       this.mChildren.add(i4search);return this;
/*     */     }
/*     */     
/*     */     boolean canBeRunLocally() {
/*  77 */       for (ImapSearch i4search : this.mChildren) {
/*  78 */         if (!i4search.canBeRunLocally()) {
/*  79 */           return false;
/*     */         }
/*     */       }
/*  82 */       return true;
/*     */     }
/*     */     
/*     */     boolean requiresMODSEQ() {
/*  86 */       for (ImapSearch i4search : this.mChildren) {
/*  87 */         if (i4search.requiresMODSEQ()) {
/*  88 */           return true;
/*     */         }
/*     */       }
/*  91 */       return false;
/*     */     }
/*     */     
/*     */     public int hashCode()
/*     */     {
/*  96 */       int prime = 31;
/*  97 */       int result = 1;
/*  98 */       result = 31 * result + (this.mChildren == null ? 0 : this.mChildren.hashCode());
/*  99 */       return result;
/*     */     }
/*     */     
/*     */     public boolean equals(Object obj)
/*     */     {
/* 104 */       if (this == obj) {
/* 105 */         return true;
/*     */       }
/* 107 */       if (obj == null) {
/* 108 */         return false;
/*     */       }
/* 110 */       if (getClass() != obj.getClass()) {
/* 111 */         return false;
/*     */       }
/* 113 */       LogicalOperation other = (LogicalOperation)obj;
/* 114 */       if (this.mChildren == null) {
/* 115 */         if (other.mChildren != null) {
/* 116 */           return false;
/*     */         }
/* 118 */       } else if (!this.mChildren.equals(other.mChildren)) {
/* 119 */         return false;
/*     */       }
/* 121 */       return true;
/*     */     }
/*     */   }
/*     */   
/*     */   static class AndOperation extends ImapSearch.LogicalOperation {
/* 126 */     AndOperation(ImapSearch... children) { for (ImapSearch i4search : children) addChild(i4search);
/*     */     }
/*     */     
/* 129 */     String toZimbraSearch(ImapFolder i4folder) throws ImapParseException { StringBuilder search = new StringBuilder("(");
/* 130 */       for (ImapSearch i4search : this.mChildren) {
/* 131 */         search.append(search.length() == 1 ? "" : " ").append(i4search.toZimbraSearch(i4folder));
/*     */       }
/* 133 */       return ')';
/*     */     }
/*     */     
/*     */     ImapMessage.ImapMessageSet evaluate(ImapFolder i4folder) throws ImapParseException {
/* 137 */       ImapMessage.ImapMessageSet matched = null;
/* 138 */       for (ImapSearch i4search : this.mChildren) {
/* 139 */         if (matched == null) {
/* 140 */           matched = i4search.evaluate(i4folder);
/*     */         } else {
/* 142 */           matched.retainAll(i4search.evaluate(i4folder));
/*     */         }
/*     */         
/* 145 */         if (matched.isEmpty())
/*     */           break;
/*     */       }
/* 148 */       return matched;
/*     */     }
/*     */   }
/*     */   
/*     */   static class OrOperation extends ImapSearch.LogicalOperation {
/* 153 */     OrOperation(ImapSearch... children) { for (ImapSearch i4search : children) addChild(i4search);
/*     */     }
/*     */     
/* 156 */     String toZimbraSearch(ImapFolder i4folder) throws ImapParseException { StringBuilder search = new StringBuilder("(");
/* 157 */       for (ImapSearch i4search : this.mChildren) {
/* 158 */         search.append(search.length() == 1 ? "(" : " or (").append(i4search.toZimbraSearch(i4folder)).append(')');
/*     */       }
/* 160 */       return ')';
/*     */     }
/*     */     
/*     */     ImapMessage.ImapMessageSet evaluate(ImapFolder i4folder) throws ImapParseException {
/* 164 */       ImapMessage.ImapMessageSet matched = null;
/* 165 */       for (ImapSearch i4search : this.mChildren) {
/* 166 */         if (matched == null) {
/* 167 */           matched = i4search.evaluate(i4folder);
/*     */         } else {
/* 169 */           matched.addAll(i4search.evaluate(i4folder));
/*     */         }
/*     */         
/* 172 */         if (isAllMessages(i4folder, matched))
/*     */           break;
/*     */       }
/* 175 */       return matched;
/*     */     }
/*     */   }
/*     */   
/*     */   static class NotOperation extends ImapSearch.LogicalOperation { NotOperation() {}
/*     */     
/* 181 */     NotOperation(ImapSearch child) { addChild(child); }
/*     */     
/*     */     String toZimbraSearch(ImapFolder i4folder) throws ImapParseException {
/* 184 */       return '-' + ((ImapSearch)this.mChildren.get(0)).toZimbraSearch(i4folder);
/*     */     }
/*     */     
/*     */     ImapMessage.ImapMessageSet evaluate(ImapFolder i4folder) throws ImapParseException {
/* 188 */       ImapMessage.ImapMessageSet matches = i4folder.getAllMessages();
/* 189 */       matches.removeAll(((ImapSearch)this.mChildren.get(0)).evaluate(i4folder));
/* 190 */       return matches;
/*     */     }
/*     */   }
/*     */   
/*     */   static class AllSearch extends ImapSearch {
/* 195 */     boolean canBeRunLocally() { return true; }
/* 196 */     String toZimbraSearch(ImapFolder i4folder) { return "item:all"; }
/* 197 */     ImapMessage.ImapMessageSet evaluate(ImapFolder i4folder) { return i4folder.getAllMessages(); }
/*     */     
/*     */     public boolean equals(Object obj)
/*     */     {
/* 201 */       return obj instanceof AllSearch;
/*     */     }
/*     */     
/*     */     public int hashCode()
/*     */     {
/* 206 */       return getClass().hashCode();
/*     */     }
/*     */   }
/*     */   
/*     */   static class NoneSearch extends ImapSearch {
/* 211 */     boolean canBeRunLocally() { return true; }
/* 212 */     String toZimbraSearch(ImapFolder i4folder) { return "item:none"; }
/* 213 */     ImapMessage.ImapMessageSet evaluate(ImapFolder i4folder) { return new ImapMessage.ImapMessageSet(); }
/*     */     
/*     */     public boolean equals(Object obj)
/*     */     {
/* 217 */       return obj instanceof NoneSearch;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 222 */     public int hashCode() { return getClass().hashCode(); }
/*     */   }
/*     */   
/*     */   static class SequenceSearch extends ImapSearch {
/*     */     private String mTag;
/*     */     private String mSubSequence;
/*     */     private boolean mIsUidSearch;
/*     */     
/* 230 */     SequenceSearch(String tag, String subSequence, boolean byUID) { this.mTag = tag;this.mSubSequence = subSequence;this.mIsUidSearch = byUID; }
/*     */     
/* 232 */     boolean canBeRunLocally() { return true; }
/*     */     
/*     */     String toZimbraSearch(ImapFolder i4folder) throws ImapParseException {
/* 235 */       return sequenceAsSearchTerm(i4folder, evaluate(i4folder), true);
/*     */     }
/*     */     
/*     */     ImapMessage.ImapMessageSet evaluate(ImapFolder i4folder) throws ImapParseException {
/* 239 */       return i4folder.getSubsequence(this.mTag, this.mSubSequence, this.mIsUidSearch, true);
/*     */     }
/*     */     
/*     */     public int hashCode()
/*     */     {
/* 244 */       int prime = 31;
/* 245 */       int result = 1;
/* 246 */       result = 31 * result + (this.mIsUidSearch ? 1231 : 1237);
/* 247 */       result = 31 * result + (this.mSubSequence == null ? 0 : this.mSubSequence.hashCode());
/* 248 */       return result;
/*     */     }
/*     */     
/*     */     public boolean equals(Object obj)
/*     */     {
/* 253 */       if (this == obj) {
/* 254 */         return true;
/*     */       }
/* 256 */       if (obj == null) {
/* 257 */         return false;
/*     */       }
/* 259 */       if (getClass() != obj.getClass()) {
/* 260 */         return false;
/*     */       }
/* 262 */       SequenceSearch other = (SequenceSearch)obj;
/* 263 */       if (this.mIsUidSearch != other.mIsUidSearch) {
/* 264 */         return false;
/*     */       }
/* 266 */       if (this.mSubSequence == null) {
/* 267 */         if (other.mSubSequence != null) {
/* 268 */           return false;
/*     */         }
/* 270 */       } else if (!this.mSubSequence.equals(other.mSubSequence)) {
/* 271 */         return false;
/*     */       }
/* 273 */       return true;
/*     */     }
/*     */   }
/*     */   
/*     */   static class FlagSearch extends ImapSearch { private String mFlagName;
/*     */     
/* 279 */     FlagSearch(String flagName) { this.mFlagName = flagName; }
/*     */     
/* 281 */     boolean canBeRunLocally() { return true; }
/*     */     
/*     */     String toZimbraSearch(ImapFolder i4folder) {
/* 284 */       ImapFlagCache.ImapFlag i4flag = i4folder.getFlagByName(this.mFlagName);
/* 285 */       if (i4flag == null) {
/* 286 */         return "item:none";
/*     */       }
/* 288 */       String prefix = i4flag.mPositive ? "" : "(-";String suffix = i4flag.mPositive ? "" : ")";
/* 289 */       if (i4flag.mPermanent) {
/* 290 */         return prefix + "tag:" + i4flag.mName + suffix;
/*     */       }
/* 292 */       return prefix + sequenceAsSearchTerm(i4folder, i4folder.getFlaggedMessages(i4flag), true) + suffix;
/*     */     }
/*     */     
/*     */     ImapMessage.ImapMessageSet evaluate(ImapFolder i4folder) {
/* 296 */       ImapFlagCache.ImapFlag i4flag = i4folder.getFlagByName(this.mFlagName);
/* 297 */       if (i4flag == null)
/* 298 */         return new ImapMessage.ImapMessageSet();
/* 299 */       if (i4flag.mPositive) {
/* 300 */         return i4folder.getFlaggedMessages(i4flag);
/*     */       }
/* 302 */       ImapMessage.ImapMessageSet matched = i4folder.getAllMessages();
/* 303 */       matched.removeAll(i4folder.getFlaggedMessages(i4flag));
/* 304 */       return matched;
/*     */     }
/*     */     
/*     */ 
/*     */     public int hashCode()
/*     */     {
/* 310 */       int prime = 31;
/* 311 */       int result = 1;
/* 312 */       result = 31 * result + (this.mFlagName == null ? 0 : this.mFlagName.hashCode());
/* 313 */       return result;
/*     */     }
/*     */     
/*     */     public boolean equals(Object obj)
/*     */     {
/* 318 */       if (this == obj) {
/* 319 */         return true;
/*     */       }
/* 321 */       if (obj == null) {
/* 322 */         return false;
/*     */       }
/* 324 */       if (getClass() != obj.getClass()) {
/* 325 */         return false;
/*     */       }
/* 327 */       FlagSearch other = (FlagSearch)obj;
/* 328 */       if (this.mFlagName == null) {
/* 329 */         if (other.mFlagName != null) {
/* 330 */           return false;
/*     */         }
/* 332 */       } else if (!this.mFlagName.equals(other.mFlagName)) {
/* 333 */         return false;
/*     */       }
/* 335 */       return true; } }
/*     */   
/*     */   static class DateSearch extends ImapSearch { private Relation mRelation;
/*     */     private Date mDate;
/*     */     private long mTimestamp;
/*     */     
/* 341 */     static enum Relation { before("before:"),  after("date:>="),  date("date:");
/*     */       
/*     */ 
/* 344 */       private Relation(String rep) { this.query = rep; }
/* 345 */       public String toString() { return this.query; }
/*     */       
/*     */       String query;
/*     */     }
/*     */     
/* 350 */     DateSearch(Relation relation, Date date) { this.mDate = date;this.mTimestamp = date.getTime();this.mRelation = relation;
/*     */     }
/*     */     
/* 353 */     boolean canBeRunLocally() { return (this.mTimestamp < 0L) || (this.mTimestamp > System.currentTimeMillis() + 96422400000L); }
/*     */     
/*     */     String toZimbraSearch(ImapFolder i4folder)
/*     */     {
/* 357 */       if (this.mTimestamp < 0L)
/* 358 */         return this.mRelation == Relation.after ? "item:all" : "item:none";
/* 359 */       if (this.mTimestamp > System.currentTimeMillis() + 96422400000L) {
/* 360 */         return this.mRelation == Relation.before ? "item:all" : "item:none";
/*     */       }
/* 362 */       return this.mRelation + DateFormat.getDateInstance(3).format(this.mDate);
/*     */     }
/*     */     
/*     */     ImapMessage.ImapMessageSet evaluate(ImapFolder i4folder)
/*     */     {
/* 367 */       if (this.mTimestamp < 0L)
/* 368 */         return this.mRelation == Relation.after ? i4folder.getAllMessages() : new ImapMessage.ImapMessageSet();
/* 369 */       if (this.mTimestamp > System.currentTimeMillis() + 96422400000L) {
/* 370 */         return this.mRelation == Relation.before ? i4folder.getAllMessages() : new ImapMessage.ImapMessageSet();
/*     */       }
/* 372 */       throw new UnsupportedOperationException("evaluate of " + toZimbraSearch(i4folder));
/*     */     }
/*     */     
/*     */ 
/*     */     public int hashCode()
/*     */     {
/* 378 */       int prime = 31;
/* 379 */       int result = 1;
/* 380 */       result = 31 * result + (this.mDate == null ? 0 : this.mDate.hashCode());
/* 381 */       result = 31 * result + (this.mRelation == null ? 0 : this.mRelation.hashCode());
/* 382 */       result = 31 * result + (int)(this.mTimestamp ^ this.mTimestamp >>> 32);
/* 383 */       return result;
/*     */     }
/*     */     
/*     */     public boolean equals(Object obj)
/*     */     {
/* 388 */       if (this == obj) {
/* 389 */         return true;
/*     */       }
/* 391 */       if (obj == null) {
/* 392 */         return false;
/*     */       }
/* 394 */       if (getClass() != obj.getClass()) {
/* 395 */         return false;
/*     */       }
/* 397 */       DateSearch other = (DateSearch)obj;
/* 398 */       if (this.mDate == null) {
/* 399 */         if (other.mDate != null) {
/* 400 */           return false;
/*     */         }
/* 402 */       } else if (!this.mDate.equals(other.mDate)) {
/* 403 */         return false;
/*     */       }
/* 405 */       if (this.mRelation == null) {
/* 406 */         if (other.mRelation != null) {
/* 407 */           return false;
/*     */         }
/* 409 */       } else if (!this.mRelation.equals(other.mRelation)) {
/* 410 */         return false;
/*     */       }
/* 412 */       if (this.mTimestamp != other.mTimestamp) {
/* 413 */         return false;
/*     */       }
/* 415 */       return true;
/*     */     }
/*     */   }
/*     */   
/*     */   static class RelativeDateSearch extends ImapSearch { private ImapSearch.DateSearch.Relation mRelation;
/*     */     private int mOffset;
/*     */     
/* 422 */     RelativeDateSearch(ImapSearch.DateSearch.Relation relation, int offset) { this.mOffset = offset;this.mRelation = relation; }
/*     */     
/* 424 */     boolean canBeRunLocally() { return false; }
/* 425 */     String toZimbraSearch(ImapFolder i4folder) { return this.mRelation.toString() + (System.currentTimeMillis() - this.mOffset * 1000L); }
/*     */     
/* 427 */     ImapMessage.ImapMessageSet evaluate(ImapFolder i4folder) { throw new UnsupportedOperationException("evaluate of " + toZimbraSearch(i4folder)); }
/*     */     
/*     */ 
/*     */     public int hashCode()
/*     */     {
/* 432 */       int prime = 31;
/* 433 */       int result = 1;
/* 434 */       result = 31 * result + this.mOffset;
/* 435 */       result = 31 * result + (this.mRelation == null ? 0 : this.mRelation.hashCode());
/* 436 */       return result;
/*     */     }
/*     */     
/*     */     public boolean equals(Object obj)
/*     */     {
/* 441 */       if (this == obj) {
/* 442 */         return true;
/*     */       }
/* 444 */       if (obj == null) {
/* 445 */         return false;
/*     */       }
/* 447 */       if (getClass() != obj.getClass()) {
/* 448 */         return false;
/*     */       }
/* 450 */       RelativeDateSearch other = (RelativeDateSearch)obj;
/* 451 */       if (this.mOffset != other.mOffset) {
/* 452 */         return false;
/*     */       }
/* 454 */       if (this.mRelation == null) {
/* 455 */         if (other.mRelation != null) {
/* 456 */           return false;
/*     */         }
/* 458 */       } else if (!this.mRelation.equals(other.mRelation)) {
/* 459 */         return false;
/*     */       }
/* 461 */       return true;
/*     */     }
/*     */   }
/*     */   
/*     */   static class ModifiedSearch extends ImapSearch { private int mChangedSince;
/*     */     
/* 467 */     ModifiedSearch(int changeId) { this.mChangedSince = changeId; }
/*     */     
/* 469 */     boolean requiresMODSEQ() { return true; }
/* 470 */     boolean canBeRunLocally() { return false; }
/* 471 */     String toZimbraSearch(ImapFolder i4folder) { return "(modseq:>" + this.mChangedSince + ")"; }
/*     */     
/* 473 */     ImapMessage.ImapMessageSet evaluate(ImapFolder i4folder) { throw new UnsupportedOperationException("evaluate of " + toZimbraSearch(i4folder)); }
/*     */     
/*     */ 
/*     */     public int hashCode()
/*     */     {
/* 478 */       int prime = 31;
/* 479 */       int result = 1;
/* 480 */       result = 31 * result + this.mChangedSince;
/* 481 */       return result;
/*     */     }
/*     */     
/*     */     public boolean equals(Object obj)
/*     */     {
/* 486 */       if (this == obj) {
/* 487 */         return true;
/*     */       }
/* 489 */       if (obj == null) {
/* 490 */         return false;
/*     */       }
/* 492 */       if (getClass() != obj.getClass()) {
/* 493 */         return false;
/*     */       }
/* 495 */       ModifiedSearch other = (ModifiedSearch)obj;
/* 496 */       if (this.mChangedSince != other.mChangedSince) {
/* 497 */         return false;
/*     */       }
/* 499 */       return true; } }
/*     */   
/*     */   static class SizeSearch extends ImapSearch { private Relation mRelation;
/*     */     private long mSize;
/*     */     
/* 504 */     static enum Relation { larger,  smaller;
/*     */       
/*     */       private Relation() {} }
/* 507 */     SizeSearch(Relation relation, long size) { this.mSize = size;this.mRelation = relation; }
/*     */     
/* 509 */     boolean canBeRunLocally() { return false; }
/* 510 */     String toZimbraSearch(ImapFolder i4folder) { return this.mRelation + ":" + this.mSize; }
/*     */     
/* 512 */     ImapMessage.ImapMessageSet evaluate(ImapFolder i4folder) { throw new UnsupportedOperationException("evaluate of " + toZimbraSearch(i4folder)); }
/*     */     
/*     */ 
/*     */     public int hashCode()
/*     */     {
/* 517 */       int prime = 31;
/* 518 */       int result = 1;
/* 519 */       result = 31 * result + (this.mRelation == null ? 0 : this.mRelation.hashCode());
/* 520 */       result = 31 * result + (int)(this.mSize ^ this.mSize >>> 32);
/* 521 */       return result;
/*     */     }
/*     */     
/*     */     public boolean equals(Object obj)
/*     */     {
/* 526 */       if (this == obj) {
/* 527 */         return true;
/*     */       }
/* 529 */       if (obj == null) {
/* 530 */         return false;
/*     */       }
/* 532 */       if (getClass() != obj.getClass()) {
/* 533 */         return false;
/*     */       }
/* 535 */       SizeSearch other = (SizeSearch)obj;
/* 536 */       if (this.mRelation == null) {
/* 537 */         if (other.mRelation != null) {
/* 538 */           return false;
/*     */         }
/* 540 */       } else if (!this.mRelation.equals(other.mRelation)) {
/* 541 */         return false;
/*     */       }
/* 543 */       if (this.mSize != other.mSize) {
/* 544 */         return false;
/*     */       }
/* 546 */       return true;
/*     */     }
/*     */   }
/*     */   
/*     */   static class ContentSearch extends ImapSearch { private String mValue;
/*     */     
/* 552 */     ContentSearch(String value) { this.mValue = value; }
/*     */     
/* 554 */     boolean canBeRunLocally() { return this.mValue.trim().equals(""); }
/* 555 */     String toZimbraSearch(ImapFolder i4folder) { return stringAsSearchTerm(this.mValue); }
/*     */     
/* 557 */     ImapMessage.ImapMessageSet evaluate(ImapFolder i4folder) { if (canBeRunLocally())
/* 558 */         return i4folder.getAllMessages();
/* 559 */       throw new UnsupportedOperationException("evaluate of " + toZimbraSearch(i4folder));
/*     */     }
/*     */     
/*     */     public int hashCode()
/*     */     {
/* 564 */       int prime = 31;
/* 565 */       int result = 1;
/* 566 */       result = 31 * result + (this.mValue == null ? 0 : this.mValue.hashCode());
/* 567 */       return result;
/*     */     }
/*     */     
/*     */     public boolean equals(Object obj)
/*     */     {
/* 572 */       if (this == obj) {
/* 573 */         return true;
/*     */       }
/* 575 */       if (obj == null) {
/* 576 */         return false;
/*     */       }
/* 578 */       if (getClass() != obj.getClass()) {
/* 579 */         return false;
/*     */       }
/* 581 */       ContentSearch other = (ContentSearch)obj;
/* 582 */       if (this.mValue == null) {
/* 583 */         if (other.mValue != null) {
/* 584 */           return false;
/*     */         }
/* 586 */       } else if (!this.mValue.equals(other.mValue)) {
/* 587 */         return false;
/*     */       }
/* 589 */       return true;
/*     */     } }
/*     */   
/*     */   static class HeaderSearch extends ImapSearch { private Header mHeader;
/*     */     private String mValue;
/*     */     
/* 595 */     static class Header { static final Header SUBJECT = new Header("subject", "subject");
/* 596 */       static final Header FROM = new Header("from", "from");
/* 597 */       static final Header TO = new Header("to", "to");
/* 598 */       static final Header CC = new Header("cc", "cc");
/* 599 */       static final Header BCC = new Header("bcc", "#bcc");
/* 600 */       static final Header MSGID = new Header("message-id", "msgid");
/*     */       
/* 602 */       private static final Header[] SPECIAL_HEADERS = { SUBJECT, FROM, TO, CC, BCC, MSGID };
/*     */       private final String mField;
/*     */       private final String mKey;
/*     */       
/* 606 */       private Header(String fieldName, String searchKey) { this.mField = fieldName;this.mKey = searchKey;
/*     */       }
/*     */       
/*     */       static Header parse(String field) {
/* 610 */         field = field.toLowerCase();
/* 611 */         for (Header syshdr : SPECIAL_HEADERS) {
/* 612 */           if (field.equals(syshdr.mField))
/* 613 */             return syshdr;
/*     */         }
/* 615 */         String key = field.replaceAll("[ \t\":()]", "");
/* 616 */         while (key.startsWith("-"))
/* 617 */           key = key.substring(1);
/* 618 */         if (key.equals(""))
/* 619 */           key = "*";
/* 620 */         return new Header(field, '#' + key);
/*     */       }
/*     */       
/* 623 */       public String toString() { return this.mKey; }
/*     */       
/*     */       public int hashCode()
/*     */       {
/* 627 */         int prime = 31;
/* 628 */         int result = 1;
/* 629 */         result = 31 * result + (this.mField == null ? 0 : this.mField.hashCode());
/* 630 */         result = 31 * result + (this.mKey == null ? 0 : this.mKey.hashCode());
/* 631 */         return result;
/*     */       }
/*     */       
/*     */       public boolean equals(Object obj)
/*     */       {
/* 636 */         if (this == obj) {
/* 637 */           return true;
/*     */         }
/* 639 */         if (obj == null) {
/* 640 */           return false;
/*     */         }
/* 642 */         if (getClass() != obj.getClass()) {
/* 643 */           return false;
/*     */         }
/* 645 */         Header other = (Header)obj;
/* 646 */         if (this.mField == null) {
/* 647 */           if (other.mField != null) {
/* 648 */             return false;
/*     */           }
/* 650 */         } else if (!this.mField.equals(other.mField)) {
/* 651 */           return false;
/*     */         }
/* 653 */         if (this.mKey == null) {
/* 654 */           if (other.mKey != null) {
/* 655 */             return false;
/*     */           }
/* 657 */         } else if (!this.mKey.equals(other.mKey)) {
/* 658 */           return false;
/*     */         }
/* 660 */         return true;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 666 */     private boolean mPrefixSearch = true;
/*     */     
/* 668 */     HeaderSearch(Header header, String value) { while ((value.startsWith("<")) || (value.startsWith(">")) || (value.startsWith("="))) {
/* 669 */         value = value.substring(1);
/*     */       }
/* 671 */       if ((header == Header.MSGID) && (value.endsWith(">"))) {
/* 672 */         value = value.substring(0, value.length() - 1);
/* 673 */         this.mPrefixSearch = false;
/*     */       }
/* 675 */       this.mValue = value;this.mHeader = header;
/*     */     }
/*     */     
/* 678 */     boolean canBeRunLocally() { return false; }
/*     */     
/* 680 */     String toZimbraSearch(ImapFolder i4folder) { String value = stringAsSearchTerm(this.mValue, this.mPrefixSearch);
/* 681 */       if (((this.mHeader == Header.FROM) || (this.mHeader == Header.TO) || (this.mHeader == Header.CC)) && (this.mValue.indexOf('@') == -1)) {
/* 682 */         value = value + " or " + stringAsSearchTerm(new StringBuilder().append('@').append(this.mValue).toString());
/*     */       }
/* 684 */       return this.mHeader + ":(" + value + ")";
/*     */     }
/*     */     
/* 687 */     ImapMessage.ImapMessageSet evaluate(ImapFolder i4folder) { throw new UnsupportedOperationException("evaluate of " + toZimbraSearch(i4folder)); }
/*     */     
/*     */ 
/*     */     public int hashCode()
/*     */     {
/* 692 */       int prime = 31;
/* 693 */       int result = 1;
/* 694 */       result = 31 * result + (this.mHeader == null ? 0 : this.mHeader.hashCode());
/* 695 */       result = 31 * result + (this.mPrefixSearch ? 1231 : 1237);
/* 696 */       result = 31 * result + (this.mValue == null ? 0 : this.mValue.hashCode());
/* 697 */       return result;
/*     */     }
/*     */     
/*     */     public boolean equals(Object obj)
/*     */     {
/* 702 */       if (this == obj) {
/* 703 */         return true;
/*     */       }
/* 705 */       if (obj == null) {
/* 706 */         return false;
/*     */       }
/* 708 */       if (getClass() != obj.getClass()) {
/* 709 */         return false;
/*     */       }
/* 711 */       HeaderSearch other = (HeaderSearch)obj;
/* 712 */       if (this.mHeader == null) {
/* 713 */         if (other.mHeader != null) {
/* 714 */           return false;
/*     */         }
/* 716 */       } else if (!this.mHeader.equals(other.mHeader)) {
/* 717 */         return false;
/*     */       }
/* 719 */       if (this.mPrefixSearch != other.mPrefixSearch) {
/* 720 */         return false;
/*     */       }
/* 722 */       if (this.mValue == null) {
/* 723 */         if (other.mValue != null) {
/* 724 */           return false;
/*     */         }
/* 726 */       } else if (!this.mValue.equals(other.mValue)) {
/* 727 */         return false;
/*     */       }
/* 729 */       return true;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/imap/ImapSearch.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */